//字符串工具
const chars = [
  "0",
  "1",
  "2",
  "3",
  "4",
  "5",
  "6",
  "7",
  "8",
  "9",
  "A",
  "B",
  "C",
  "D",
  "E",
  "F",
  "G",
  "H",
  "I",
  "J",
  "K",
  "L",
  "M",
  "N",
  "O",
  "P",
  "Q",
  "R",
  "S",
  "T",
  "U",
  "V",
  "W",
  "X",
  "Y",
  "Z",
  "a",
  "b",
  "c",
  "d",
  "e",
  "f",
  "g",
  "h",
  "i",
  "j",
  "k",
  "l",
  "m",
  "n",
  "o",
  "p",
  "q",
  "r",
  "s",
  "t",
  "u",
  "v",
  "w",
  "x",
  "y",
  "z"
];

const numChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
const numUnit = ["", "十", "百", "千"]; //权位
const numSection = ["", "万", "亿", "万亿", "亿亿"]; //节权位

/**
 * 获取长度为num的随机字符串，可能包含A-Za-z0-9
 * @param {int} num
 */
const generateMixed = num => {
  let res = "";
  for (let i = 0; i < num; i++) {
    let id = Math.ceil(Math.random() * 35);
    res += chars[id];
  }
  return res;
};

// 生成一个随机数
const randomNum = (min, max) => {
  return Math.floor(Math.random() * (max - min) + min);
};

/**
 * 判断字符串是否为空，若为空则返回true
 * @param {string} str
 */
const isBlank = str => {
  return (
    str == null ||
    typeof str == "undefined" ||
    str.length === 0 ||
    str.replace(/(^s*)|(s*$)/g, "").length === 0 ||
    str === ""
  );
};

/**
 * 判断任意个字符串是否为空，只要有一个为空则返回false
 * @param  {...string} str
 */
const notBlank = (...str) => {
  if (str == null || str.length == 0) {
    return false;
  }
  for (let i = 0; i < str.length; i++) {
    if (isBlank(str[i])) {
      return false;
    }
  }
  return true;
};

/**
 * 获取格式化的当前日期 yyyy-MM-dd HH:mm:ss
 */
const getDateTime = () => {
  let now = new Date();
  let str =
    now.getFullYear() +
    "-" +
    addZero(now.getMonth() + 1) +
    "-" +
    addZero(now.getDate()) +
    " " +
    addZero(now.getHours()) +
    ":" +
    addZero(now.getMinutes()) +
    ":" +
    addZero(now.getSeconds());
  return str;
};

/**
 * 获取格式化的当前日期 yyyy-MM-dd
 */
const getDate = () => {
  let now = new Date();
  let str =
    now.getFullYear() +
    "-" +
    addZero(now.getMonth() + 1) +
    "-" +
    addZero(now.getDate());
  return str;
};

/**
 * 获取格式化的当前日期 HH:mm:ss
 */
const getTime = () => {
  let now = new Date();
  let str =
    addZero(now.getHours()) +
    ":" +
    addZero(now.getMinutes()) +
    ":" +
    addZero(now.getSeconds());
  return str;
};

/**
 * 判断num是否小于10，小于10则在前面填一个0
 * @param {int} num
 */
const addZero = num => {
  if (num < 10) {
    return "0" + num;
  } else {
    return num + "";
  }
};

const isJSON = str => {
  if (typeof str == "string") {
    try {
      JSON.parse(str);
      return true;
    } catch (e) {
      return false;
    }
  } else {
    return false;
  }
};

//数组数据去重
const distinct = (arr, key) => {
  let newArrry = [];
  let obj = {};
  if (key) {
    //根据属性判断重复
    let len = arr.length;
    for (let i = 0; i < len; i++) {
      if (obj[arr[i][key]] !== 1) {
        newArrry.push(arr[i]);
        obj[arr[i][key]] = 1;
      }
    }
  } else {
    //根据整体判断重复
    let len = arr.length;
    for (let i = 0; i < len; i++) {
      if (obj[arr[i]] !== 1) {
        newArrry.push(arr[i]);
        obj[arr[i]] = 1;
      }
    }
  }
  return newArrry;
};

const formatSection = num => {
  let arr = (num + "").split("").reverse();
  let str = "";
  for (let i = 0; i < arr.length; i++) {
    //将0-9转化为零到九
    let char = arr[i] == 0 ? numChar[0] : numChar[arr[i]] + numUnit[i]; //当数字为0时不加权位，非零加权位
    str = char + str;
  }
  let s = str.replace(/零+/g, "零").replace(/零+$/, ""); //将多个零合并为一个零，并剔除尾端的零
  return s;
};
const formatNum = (num, str) => {
  //将字符串按个数拆分
  let len = Math.ceil(str.length / num);
  let arr = [];
  for (let i = 0; i < len; i++) {
    let reverseStr = str
      .split("")
      .reverse()
      .join("");
    let s = reverseStr
      .slice(i * num, i * num + num)
      .split("")
      .reverse()
      .join("");
    arr.unshift(s);
  }
  return arr;
};

const numberTranToCN = num => {
  let arr = formatNum(4, num + ""); //将数字每四个拆分一次
  let list = [];
  for (let i = 0; i < arr.length; i++) {
    let str = formatSection(arr[i]);
    list.push(str);
  }
  let reverseList = list.reverse();
  for (let j = 0; j < reverseList.length; j++) {
    reverseList[j] += numSection[j];
  }
  return reverseList.reverse().join("");
};

export default {
  generateMixed,
  isBlank,
  notBlank,
  getDateTime,
  getDate,
  getTime,
  randomNum,
  isJSON,
  distinct,
  numberTranToCN
};
